package com.tonny.tweet.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.tonny.tweet.dao.UserDao;
import com.tonny.tweet.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate template;
    
    @Override
    public int insert(User user) {
        return template.update("INSERT INTO USER(username, password, status, email) VALUES(?, ?, ?, ?)", user.getUsername(), user.getPassword(),
                               user.getStatus(), user.getEmail());
    }
    
    @Override
    public long countByUserName(String username) {
        return template.queryForObject("select count(*) from user where username = ?", new String[]{username}, Long.class);
    }
    
    @Override
    public User queryByUserName(String username) {
        try {
            return template.queryForObject("select * from user where username = ?", new String[]{username}, new RowMapper<User>() {
                @Override
                public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return parseFrom(rs);
                }
            });
        }
        catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
    
    private User parseFrom(ResultSet rs) throws SQLException {
        User dbUser = new User();
        dbUser.setId(rs.getLong("id"));
        dbUser.setUsername(rs.getString("username"));
        dbUser.setPassword(rs.getString("password"));
        dbUser.setEmail(rs.getString("email"));
        dbUser.setStatus(rs.getInt("status"));
        dbUser.setCreateTime(rs.getTimestamp("create_time"));
        dbUser.setUpdateTime(rs.getTimestamp("update_time"));
        return dbUser;
    }
    
    @Override
    public User queryByUserNameAndPassword(String username, String password) {
        try {
            return template.queryForObject("select * from user where username = ? AND password = ?", new String[]{username, password},
                                           new RowMapper<User>() {
                                               @Override
                                               public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                                                   return parseFrom(rs);
                                               }
                                           });
        }
        catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
    
    @Override
    public int update(User user) {
        return template.update("UPDATE USER SET username = ?, password = ?, email = ?, status = ?, update_time = ? WHERE id = ?", user.getUsername(),
                        user.getPassword(), user.getEmail(), user.getStatus(), new Date(), user.getId());
    }
}
